package com.sdkj.action.sys;

import java.io.File;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sdkj.Entity.sys.NavMenu;
import com.sdkj.action.base.BaseAction;
import com.sdkj.service.sys.INavMenuService;
import com.sdkj.utils.CommonUtils;
import com.sdkj.utils.PageBean;

@Controller
@Scope("prototype")
public class NavMenuAction extends BaseAction<NavMenu, INavMenuService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void saveFile() {
		// 获取上传是要传到哪的路径
		ServletContext sc = ServletActionContext.getServletContext();
		String path = sc.getRealPath("/pages/font/up/");
		// 根据文件的真实名字创建一个文件
		File dest = new File(path, fileFileName);
		JSONObject j = new JSONObject();
		try {
			int no = Integer.parseInt(sc.getAttribute("imgSortNo") + "");
			sc.setAttribute("imgSortNo", no + 1);
			// 将临时文件拷贝进创建的文件中
			FileUtils.copyFile(file, dest); 
			j.put(RES_CODE, 100);
			j.put(RES_MSG, "上传文件成功");
		} catch (Exception e) {
			j.put(RES_CODE, -100);
			j.put(RES_MSG, "上传文件失败");
			e.printStackTrace();
		}
		// 删除临时文件
		file.delete();
//		CommonUtils.writeJson(j);

	}
	
	public void save() throws SQLException{
		Serializable s=service.save(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100); 
			res.put(RES_MSG, "保存成功"); 
			res.put("id", s);
			// TODO 保存到redis
			/*redisTemplate.execute(new RedisCallback<Object>() {
	            @Override
	            public Object doInRedis(RedisConnection connection) throws DataAccessException {
	                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getId()),
	                               redisTemplate.getStringSerializer().serialize(user.getName()));
	                return null;
	            }
	        });*/
			
			try {
				operationLogService.save(getOperationLogBean("save navmenus",new String[]{"toHref"}));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "保存失败");
		}
		CommonUtils.writeJson(res);
	}

	 
	public void list() throws SQLException{  
		
		JSONObject res=new JSONObject();
		List<NavMenu> data=null;
		PageBean pb=null;
		
		if(data==null){//如果redis 中没有找到数据
			String hql=" from NavMenu order by createTime desc";
			
			Map<String,Object>params=new HashMap<String,Object>();
			pb=new PageBean(page, service.count("select count(*) "+hql,params), rows);
			data=service.find(hql, params, page, rows);
			
		}
		res.put(RES_CODE, 100);
		res.put(RES_MSG, "查询成功");
		res.put("pagebean", pb);
		res.put("data", JSONArray.fromObject(data));
		CommonUtils.writeJson(res);
	}
	
	public void update() throws SQLException{
		Serializable s=service.update(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "修改成功");
			res.put("id", s);
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "修改失败"); 
		}
		CommonUtils.writeJson(res);
	}
	public void del() throws SQLException{
		boolean flag=service.deleteIds(ids);
		JSONObject res=new JSONObject();
		if(flag){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "删除数据成功");
		}else{
			res.put(RES_CODE, -100); 
			res.put(RES_MSG, "删除数据失败");
		}
		try {
			operationLogService.save(getOperationLogBean("delete navmenus",new String[]{"toHref"}));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		CommonUtils.writeJson(res);
	}

	 //上传文件时的临时文件
	 private File file;
	 //上传文件的类型
	 private String fileContentType;
	 //上传文件的真实名称
	 private String fileFileName; 

	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

}
